Mythics
Mythics

Reputation: 803

Perl, set variable to entire command line 'string' rather than arguments

I need to allow users to type in just about anything when they run my perl script as my script is sending all data to another non-custom script at different increments.

In theory, I'm hoping this could happen:

perlscript "this is" -t stuff\n4^./q%

Then, in perlscript, have:

print "Full: $full_command";

which results in:

Full: "this is" -t stuff\n4^./q%

That make any sense? Nothing I've tried yet does exactly what I'm looking for with argv or the like.

Thanks for any help, Tim

Upvotes: 0

Views: 847

Answers (3)

schtever
schtever

Reputation: 3250

Assuming you quote correctly for your OS, as mentioned above, perhaps the following will work for you:

print join(" ", map { / / ? "\"$_\"" : $_ } @ARGV);

Note that when you run perl you need to separate the arguments that are destined for perl itself from those for your program using the "--" token. For example:

perl -- -t 'stuff...'

That will protect against arguments being mistakenly consumed by perl itself.

Upvotes: -1

ikegami
ikegami

Reputation: 385809

The shell does something equivalent to

exec('perlscript', 'this is', '-t', 'stuff'.chr(0x0A).'4&^./q%');

There's no way perl can produce the original shell command from that. If you want Perl to receive

"this is" -t stuff\n4^./q%

you need to tell the shell that using something like

perlscript '"this is" -t stuff\n4^./q%'

(Well, at least for a Borne shell or derivative.)

Upvotes: 4

mob
mob

Reputation: 118605

Unfortunately, your shell is likely to manipulate your arguments before they even get to the Perl script. In your specific example, a shell like bash would remove the quote marks, treat the \n as an n, and stop processing the line once it got to &. In Unixy systems, your best bet may be to wrap all of your arguments in single quotes

perlscript '"this is" -t stuff\n4&^./q%'

Upvotes: 2

Related Questions