jschimno has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a script that takes an email address as the first argument and sends email to users. But when I try to use $argv[1] I get errors due to the unescaped @.
Simplified script:
#!/usr/bin/perl -w print $argv[1] . "\n";
I have tried using split to separate the username from the host:  ($username, $host ) = split( '\@', $argv[1] ); Which didn't work. If anyone has any ideas they would be much appreciated.

Thanks,
Justin

Replies are listed 'Best First'.
Re: email address as argument to script
by tall_man (Parson) on Mar 14, 2003 at 16:24 UTC
    Capitals matter. Use:
    print $ARGV[1] . "\n";
Re: email address as argument to script
by davorg (Chancellor) on Mar 14, 2003 at 16:24 UTC

    Are you sure your problem isn't because the first argument is in $ARGV[0] and not $argv[1]?

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: email address as argument to script
by blokhead (Monsignor) on Mar 14, 2003 at 16:24 UTC
    The name of the array is @ARGV, not @argv. Also, the fact that you must escape the @ character within double quotes in Perl has nothing to do with arguments passed to your script -- the @ symbol will appear fine within the arguments.

    blokhead

Re: email address as argument to script
by dstotten (Novice) on Mar 14, 2003 at 16:34 UTC
    I believe the problem is that you need to use $ARGV[0]. Remember that counting in perl starts with 0. So the first element in an array would be the 0'th arguement.
    #!/usr/bin/perl -w print $ARGV[0] . "\n";
    on my command line, this works
Re: email address as argument to script
by Vorlin (Beadle) on Mar 14, 2003 at 21:28 UTC
    Since this is just about argument passing, here's what I did to get what you were looking for to work.
    #!/usr/bin/perl $email = @ARGV[[0]]; # not $argv[[1]], @ARGV starts at 0 printf "%s\n", $email ? $email : "none listed";
    The above renders anything as argument 1 otherwise "none listed".

    Hope this helps. As coverage, arguments are stored in @ARGV and start at @ARGV[[0]].

    edited: Fri Mar 14 23:36:41 2003 by jeffa - code tags