Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

eval NoOp on cmd.exe line expr

by Anonymous Monk
on Mar 13, 2017 at 22:37 UTC ( [id://1184494]=perlquestion: print w/replies, xml ) Need Help??

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

Trying to replicate example 4 in "Larry's filename fixer" (Perl Cookbook, 9.9), I've tried several variants of his expr:
'print "$_: "; s/foo/bar/ if <STDIN> =~ /^y/i'
and get,for example, the following error:
The system cannot find the file specified.
Running ActivePerl-5.24 on W10, here's the code:
#!/usr/bin/perl -w use strict; use warnings; use 5.010; # Larry's filename fixer [Cookbook 9.9, mod crt for ActiveState/W10] my $op = shift or die "Usage: perl rename expr [files]\n"; chomp(@ARGV = <STDIN>) unless @ARGV; @ARGV = map glob, @ARGV; for (@ARGV) { my $was = $_; eval $op; die $@ if $@; rename ($was, $_) unless $was = $_; }
Where have I gone astray?

Replies are listed 'Best First'.
Re: eval NoOp on cmd.exe line expr
by kennethk (Abbot) on Mar 13, 2017 at 23:36 UTC
    rename ($was, $_) unless $was = $_;
    will always evaluate true because because you are using the assignment operator (=) not the string equivalence operator (eq). You should have gotten a warning about that one running the posted code.

    Reading Basic debugging checklist may be helpful. An essential element is use print/say and/or Data::Dumper to make sure variables contain what you think they should.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: eval NoOp on cmd.exe line expr
by Anonymous Monk on Mar 13, 2017 at 23:36 UTC

    Where have I gone astray?

    Well, you're forgot that cmd.exe isn't bash, and doesn't like single quotes

    See perlrun, Behind the GUI lives the Shell

    But the really real problem is you fell for Unix tool-and-filter philosophy. :)

    This is how your rename files, Re: Renaming files in a directory in sequence, make a list of renames, then human you review the list for any mistakes, then finally do the rename

    Dont assume your program is correct, esp one typed on the commandline

Re: eval NoOp on cmd.exe line expr
by haukex (Archbishop) on Mar 14, 2017 at 18:58 UTC

    This reminds me of Larry Wall's rename script, which at least on Debian systems comes with the default Perl install, at /usr/bin/prename. As far as I can tell, that's been extended upon and turned into File::Rename. Perhaps that could serve as some inspiration?

    As for what appears to be the followup question here, I think it would probably indeed be best if you could post this in a new thread, along with code that demonstrates the problem (How do I post a question effectively?).

Re: eval NoOp on cmd.exe line expr
by Anonymous Monk on Mar 14, 2017 at 18:23 UTC
    @kennethk - Thanks for spotting the =/eq bug. My transcription error, and I should have caught it, but execution never got that far, anyway;-(

    @anonymous - Thanks for your advice. So, as I suspected, my initial problem is not code but rather the cmd/perl interface.

    Larry's example actually embodied a more general issue, which is my real interest: How to pass wildcards from cmd.exe to Perl? Is it feasible or is there a more effective, Perly way? (This topic generalizes and diverges somewhat from my original title. Should it begin a new thread?)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-25 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found