Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Strings with @

by Anonymous Monk
on Mar 09, 2006 at 20:33 UTC ( #535499=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to run this script:
#!/usr/bin/perl use strict; use warnings; open IN, "<", "netapps" || die "Can't open file"; my$ recipient=some.g.guy@bigcorporation.com; while (<IN>) { chomp; system( "rsh $_ quota report | mail $recipient" ); }
and I am getting the following error?

Array found where operator expected at ./netapps.pl line 7, at end of line syntax error at ./netapps.pl line 7, near "guy@bigcorporation" Global symbol "@bigcorporation" requires explicit package name at ./netapps.pl line 7. Execution of ./netapps.pl aborted due to compilation errors.

do I need to substitute the @ in the email address for something else??

Thanks!!

2006-03-10 Retitled by g0n, as per Monastery guidelines
Original title: 'confused and befuddled......'

Replies are listed 'Best First'.
Re: Strings with @
by diotalevi (Canon) on Mar 09, 2006 at 21:16 UTC

    You also need to change how you wrote your open() call. Use either of these forms but don't use what you have now. It's terminally buggy.

    open IN, "<", "netapps" or die "Can't open file";

    open( IN, "<", "netapps" ) || die "Can't open file";

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      I prefer to get useful error messages and I really hate global filehandles and oh yeah, I prefer the 'or' style.

      open my $in, "<", "netapps" or die "Can't open file 'netapps' for read:$!";

      :-)

      ---
      $world=~s/war/peace/g

        i guess we're moving away from the OP here, but demerphq, i'm intrigued with your hatred of global filehandles and i think i like this solution ( open my $in, "file") - can you list some more benefits to this, and maybe disadvantages, that i may be overlooking?

        It's not what you look like, when you're doin' what you’re doin'.
        It's what you’re doin' when you’re doin' what you look like you’re doin'!
             - Charles Wright & the Watts 103rd Street Rhythm Band
Re: Strings with @
by shiza (Hermit) on Mar 09, 2006 at 20:47 UTC
    Actually, don't you need single quotes instead of double?
    my $recipient = 'some.g.guy@bigcorporation.com';
    You could use double quotes but you need to escape the @:
    my $recipient = "some.g.guy\@bigcorporation.com";
Re: Strings with @
by Marcello (Hermit) on Mar 09, 2006 at 20:50 UTC
    It depends, you can use single quotes like:
    my $recipient = 'some.g.guy@bigcorporation.com';
    or double quotes with a backslash before the @ character so it will not be interpolated:
    my $recipient = "some.g.guy\@bigcorporation.com";
Re: Strings with @
by ayrnieu (Beadle) on Mar 10, 2006 at 06:19 UTC
    #! /usr/bin/env perl use strict; use warnings; open my $f, 'netapsp' or die "open failed: $!"; my $recip = 'some.g.guy@bigcorp.com'; while (<$f>) { chomp; system "rsh $_ quota report | mail $recip" and die "rsh failed: $!"; }

    Morever, with such a simple program you've little excuse to not use tainting (perldoc perlsec), which would've suggested that you verify what you get from 'netapps' rather than blindly pass it to a shell.

      while (<$f>) { chomp; system "rsh $_ quota report | mail $recip" and die "rsh failed: $!"; }
      shouldn't that be or die rather than and die?

        No, it should not. Unix programs return 0 on success.

Re: Strings with @
by matze (Friar) on Mar 09, 2006 at 20:38 UTC
    Shouldn't you quote your email-address?
    my $recipient="some.g.guy\@bigcorporation.com";
    Otherwise perl is expecting some code after the =.

    Update: added escaping of '@'. Forgot about that.

      Actually he needs to use '' or q{}, otherwise the @bigcorporation will be interpreted as the name of an array and he'll get a different message.

      Update: Not to mention changing || to or, or else adding some parens in that open statement.

      Actualy that will try to find the aray @bigcorporation.
      better yet:
      my $recipient= q{some.g.guy@bigcorporation.com};
      this way no interpitation will take place.
      Ron...
Re: Strings with @
by Anonymous Monk on Mar 09, 2006 at 20:49 UTC

    Try my $receipt="some.g.guy\@bigcorporation.com";

Re: Strings with @
by Anonymous Monk on Mar 10, 2006 at 09:37 UTC
    my$ recipient=some.g.guy@bigcorporation.com;
    should be
    my $recipient = "some.g.guy\@bigcorporation.com";
Re: Strings with @
by vickym (Novice) on Mar 10, 2006 at 12:15 UTC
    The '@' symbol is used for array variable. You need to prefix the @ symbol with an escape sequence '\'. eg: my $recipient="some.g.guy\@bigcorporation.com"; Try it.
Re: Strings with @
by bravenmd (Sexton) on Mar 10, 2006 at 20:35 UTC
    First thing you need is some kind of quoting around the email address. Then depending on which quoting system you use you may need to escape the @ character. Perl is mistaking the '@bigcorporation' for an array.
    You can try either:
    'some.g.guy@bigcorporation.com',
    q~some.g.guy@bigcorporation.com~,
    q{some.g.guy@bigcorporation.com},
    "some.g.guy\@bigcorporation.com",
    etc. all of which should work
    Hope this helps!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://535499]
Approved by Corion
Front-paged by JSchmitz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2022-05-20 03:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (72 votes). Check out past polls.

    Notices?