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

UPDATE: I REALIZED MY SIMPLE MISTAKE AND FIXED IT! THANK YOU! Hi there! I've been trying to pass a string as a parameter to a program but every time I do I only get the first word. I'm thinking the program is stopping at white space but I need the whole string and I can't find the proper way to do that. Any help appreciated here is what I have so far:

my @variable = $ARGV[0]; my $test = 10; if($test == 10) { print "Testing for success: @variable \n"; }

This prints "Testing for success: Testing" but the whole string should be "Testing for success: Testing output from ExampleChecker." Im passing the string from another driver-like program to test some output for a larger program. The $test variable is just for easy manipulation for me so that's not important.

Replies are listed 'Best First'.
Re: Passing a String
by stevieb (Canon) on Nov 17, 2015 at 16:38 UTC

    I'm glad you figured out how to do what you need, but for completeness, I'll add that you can quote the string and access it as a single element (instead of slurping all of @ARGV):

    use warnings; use strict; if (! defined $ARGV[0]){ print "Usage: ./script.pl \"multi word quoted string here\"\n"; exit; } my $string = $ARGV[0]; print "$string\n"; __END__ $ ./str.pl "hello, world!" hello, world!

    Update: I was about to write up an example using Getopt::Long, but found that toolic had already written an example using it a couple of years ago. With Getopt::Long, you have the ability to add optional arguments later, unambiguously.

Re: Passing a String
by NetWallah (Canon) on Nov 17, 2015 at 16:37 UTC
    The shell splits up unquoted arguments, so try initializing like this:
    my @variable = @ARGV;
    or even eliminate @variable, and use @ARGV directly.

            “The sources of quotes found on the internet are not always reliable.” — Abraham Lincoln.3; cf.