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

I´m trying to write a perl script that reads NMEA codes from a gps whit <STDIN>, but all lines sent from the gps begins whit $ ( like $GPGLL). The sad thing is that these first six characters is the only thing i can use to sort the usable lines out,and when i try to store a string like: "$GPGLL,577.015,N,01201.076,E," into $gpsdata and i do a; system("echo $gpsdata); the result is: ",577.015,N,01201.076,E," Need i say that this is my first perl script.. :) Thanks / Johan

Replies are listed 'Best First'.
(Ovid - you're interpolating)Re: A string that begins whit $
by Ovid (Cardinal) on Apr 25, 2001 at 03:02 UTC
    Try escaping the dollar sign. You're probably having an issue with Perl interpreting $GPGLL as a scalar. Since it's undefined, you have a problem. Also, make sure you turn on warnings and strict. That will catch problems like that.
    #!/usr/bin/perl -w use strict; my $var = "$GPGLL,577.015,N,01201.076,E,"; #wrong! or my $var = "\$GPGLL,577.015,N,01201.076,E,"; #better. Note the escape. or my $var = '$GPGLL,577.015,N,01201.076,E,';
    In the final example, single quotes are used. Double quotes around a string allow for variable interpolation. Single quotes prevent that. There are a variety of ways that your code could have issues with interpolation. If you post a small snippet, we can take a look and figure out exactly how it is occurring.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: A string that begins whit $
by premchai21 (Curate) on Apr 25, 2001 at 05:47 UTC
    # Why are you doing system("echo $gpsdata") anyway? It's expensive. Try print $gpsdata, or, if you need the extra newline, print "$gpsdata\n".
(tye)Re: A string that begins whit $
by tye (Sage) on Apr 25, 2001 at 19:01 UTC

    You are telling Perl to run the command: echo $GPGLL,577.015,N,10201.076,E, go type that command in by hand and see what gets output.

    You see, Perl see that there is a $ in the command that you asked it to run so it runs /bin/sh and tells it to interpret the command for you. /bin/sh replaces $GPGLL with the value of the GPGLL environment (or shell) variable. But there is probably no such variable so the value is just the empty string.

    So, as suggested, just use: print $gpsdata,$/; instead (the $/ puts a newline on the end).

    You could also use: system("echo",$gpsdata); because when you give more than one item to system, it prevents the shell from interpreting the arguments that you asked to be passed to the command. But, in this case, the simple print is a much better choice.

    See "perldoc -f system" for more information.

            - tye (but my friends call me "Tye")