0

This script rips out the urls from a downloaded webpage. I had some trouble with this script - when I use the "my $csv_html_line = @_ ;" and then print out the "@html_LineArray" - it just prints out "1's". When I replace the "my $csv_html_line = @_ ;" with "my $csv_html_line = shift ;" the script works fine. I do not know what the difference is betweeh the "= @_" and shift - becuase I thought that without specifying something, in a subroutine, shift shift froms "@_".

#!/usr/bin/perl
use warnings;
use strict ;

sub find_url {
    my $csv_html_line = @_ ;
    #my $csv_html_line = shift ;
    my @html_LineArray = split("," , $csv_html_line ) ;
    print "@html_LineArray\n" ;
    #foreach my $split_line(@html_LineArray) {
    #    if ($split_line =~ m/"adUrl":"(http:.*)"/) {
    #        my $url = $1;
    #        $url =~ tr/\\//d;
    #        print("$url\n")  ;
    #    }
    #}
}



my $local_file = "@ARGV" ;
open(my $fh, '<', "$local_file") or die "cannot open up the $local_file $!" ;
while( my $html_line = <$fh>) {
    #print "$html_line\n";
    find_url($html_line) ;
}

This is what the above prints out.

1
1
1
1
1
1
1
1
1
1
1
1

This works fine - it uses the shift instead of "@_"

#!/usr/bin/perl
use warnings;
use strict ;

sub find_url {
    #my $csv_html_line = @_ ;
    my $csv_html_line = shift ;
    my @html_LineArray = split("," , $csv_html_line ) ;
    #print "@html_LineArray\n" ;
    foreach my $split_line(@html_LineArray) {
        if ($split_line =~ m/"adUrl":"(http:.*)"/) {
            my $url = $1;
            $url =~ tr/\\//d;
            print("$url\n")  ;
        }
    }
}



my $local_file = "@ARGV" ;
open(my $fh, '<', "$local_file") or die "cannot open up the $local_file $!" ;
while( my $html_line = <$fh>) {
    #print "$html_line\n";
    find_url($html_line) ;
}
z atef
  • 7,138
  • 3
  • 55
  • 50
capser
  • 2,442
  • 5
  • 42
  • 74
  • http://stackoverflow.com/questions/2126365/whats-the-difference-between-my-variablename-and-my-variablename-in-perl – mob Feb 22 '16 at 14:02

2 Answers2

6

It's

my ($csv_html_line) = @_ ;

The way you wrote the code you evaluated @_ in scalar context and got its length (number of elements). As you noted,

my $csv_html_line = shift;

works because the shift operator takes a list and removes and returns the first element as a scalar.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • what if I put double quotes around the special variable - my $csv_html_line = "@_ " ; – capser Feb 22 '16 at 03:36
  • 4
    Why would you want to do that? What do you believe that accomplishes? If `@_` contained more than one string, quoting it would return all the member strings concatenated, blank-separated. Probably not what you want. – Jim Garrison Feb 22 '16 at 03:38
  • Quoting the special variable will print out the whole array to the scalar, and then the scalar can be split on commas, and then placed in another array. – capser Feb 22 '16 at 03:40
  • 6
    You don't get commas, you get spaces. Why go through the trouble of concatenating then splitting again? It's already nicely split up for you. You really should be trying stuff like this out in the command-line debugger: `perl -de0` – Jim Garrison Feb 22 '16 at 03:41
2

You need

my ($csv_html_line) = @_ ;

as assigning an array to a scalar will return its length (which is 1 with one parameter)

KeepCalmAndCarryOn
  • 8,817
  • 2
  • 32
  • 47