Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

trying to pass variable through system call in windows

by mmmmms (Initiate)
on Jan 31, 2014 at 18:12 UTC ( [id://1072889]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, I'm a little new to perl. I am trying to pass a variable through my system cmd but can't get the variable through. This works and displays the value of $file1: system qq[echo ${file1}];

C:\Users\MeinerzM\Desktop\MPS_files\wotis\soc2b2014099d07.210000

However when I try to combine with a perl expression the value gets lost:

system qq["perl -i.bak -lpe \'tr/\r//d\'" ${file1}];

I get "the system could not find the specified path". Any ideas?

When I type the value directly it works fine, just have trouble passing through the variable.

Replies are listed 'Best First'.
Re: trying to pass variable through system call in windows
by toolic (Bishop) on Jan 31, 2014 at 18:23 UTC
    I don't think you need the double quotes. Try:
    system qq[perl -i.bak -lpe \'tr/\r//d\' ${file1}];

    See also:

Re: trying to pass variable through system call in windows (" not ')
by tye (Sage) on Jan 31, 2014 at 18:57 UTC
    system qq[perl -i.bak -lpe "tr/\r//d" ${file1}]

    - tye        

Re: trying to pass variable through system call in windows
by ww (Archbishop) on Jan 31, 2014 at 19:02 UTC
    IF
    ... and this is just a WAG because your description seems to be about two different actions -- the first merely printing back the name of $file1 (a variable -- perhaps 'C:\Users\MeinerzM\Desktop\MPS_files\wotis\soc2b2014099d07.210000' -- which was apparently instantiated OUTside the code you posted)...
    whereas the second seeks to modify the contents of $file1 (albeit with syntax that makes little sense to me) since there is no escaped "r" (whatever that would be) in the filename.

    But, if that's incorrect, let us know by clarifying your question(s).

    If it is correct, consider the difference: your first code will merely echo the value of the variable; not the contents of the file whose name is in your var, where the second code example apparently tries to modify the file's contents.

    C:\> perl -E "my $var = 'rstuv'; $var =~ s/r/FOO/; say $var;" FOOstuv # replaced the "r" with "FOO"... and would have done the same if $ +var were a filename; # that is, it would have changed the filename in $var, not the con +tent of the file. # Now that I've said it multiple times, does this make sense to y +ou? C:\> perl -E "my $var = 'rstuv'; $var =~ tr/r/FOO/; say $var;" Fstuv # Grossly oversimplified, the tr///ran out of "r"s to change C:\> perl -E "my $var = 'rstuv'; my $newvar = $var =~ tr/r/FOO/; say +$newvar;" 1 # count of the replacement actions

    And if you're still interested is some kind of action with a tr/// please read perldoc -f tr to see how to use it correctly.

    If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.

Re: trying to pass variable through system call in windows
by Anonymous Monk on Jan 31, 2014 at 23:12 UTC
     system $^X, qw[ -i.bak -lpe tr/\r//d ], $file1

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-19 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found