Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

moving from mac to PC

by flieckster (Scribe)
on Oct 28, 2016 at 01:30 UTC ( #1174864=perlquestion: print w/replies, xml ) Need Help??

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

Hey there, simple question but why can't i chdir on PC like i do mac? this throws out errors Can't find string terminator "'" anywhere before EOF at C:\Users\bflieck\Desktop \ line 12. Press any key to continue . . .
#!/usr/bin/env perl use File::Find; use File::Copy; use Net::SMTP; use File::Basename; use File::Slurp; use POSIX qw(strftime); my $date = strftime("%m-%d-%y",localtime); my $time = strftime("%I:%M:%S",localtime); my $findme = '\photorepos\Perl\Mother\WorkLoad\'; chdir($findme) or warn "$!";

Replies are listed 'Best First'.
Re: moving from mac to PC
by Your Mother (Archbishop) on Oct 28, 2016 at 01:48 UTC

    This is the error on that code my Mac–

    Can't find string terminator "'" anywhere before EOF at - line 10.

    In your $findme you have \';. Take the backslash out; it is escaping the quote. Also, use slashes, not backslashes. WIN understands both and everyone else uses the former.

      ... use slashes, not backslashes. WIN understand both ...

      ... except on the command line or with respect to anything handled by the command line interpreter (just for the sake of consistency):

      c:\@Work\Perl>perl -wMstrict -le "my $path = '/@Work/Perl/monks/yngwi'; my @dirs = `dir $path`; print qq{@dirs}; " Invalid switch - "@Work". c:\@Work\Perl>perl -wMstrict -le "my $path = '\@Work\Perl\monks\yngwi'; my @dirs = `dir $path`; print qq{@dirs}; " Volume in drive C is Acer Volume Serial Number is 9480-355B Directory of c:\@Work\Perl\monks\yngwi 04/02/2011 05:51 PM <DIR> . 04/02/2011 05:51 PM <DIR> .. 04/02/2011 06:18 PM 2,255 1 File(s) 2,255 bytes 2 Dir(s) 64,490,688,512 bytes free
      (This is under Win 7.)

      Give a man a fish:  <%-{-{-{-<

        ... use slashes, not backslashes. WIN understand both ...

        ... except on the command line or with respect to anything handled by the command line interpreter

        The only real problem is the command interpreter. But if you use the command interpreter, your code is already far from being portable.

        How external programs handle command line arguments depends on the individual program. Most programs with a DOS legacy treat the forward slash as switch introducing character, as do many programs written for Windows. But programs from other platforms have no problems with forward slashes in file names on the command line.


        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: moving from mac to PC (updated!)
by haukex (Bishop) on Oct 28, 2016 at 07:41 UTC

    Hi flieckster,

    For portable filename handling, I recommend the core module File::Spec, or from CPAN Path::Class:

    use Data::Dumper; $Data::Dumper::Useqq=1; $Data::Dumper::Terse=1; use File::Spec::Functions qw/catdir catpath/; print Dumper( catdir('','photorepos','Perl','Mother','WorkLoad') ); # the following volume spec is ignored in *NIX print Dumper( catpath('C:', catdir('','photorepos','Perl','Mother','WorkLoad')) ); use Path::Class qw/file dir/; print Dumper( dir('C:\\')->subdir('photorepos')->subdir('Perl') ->subdir('Mother')->subdir('WorkLoad').'' ); my $dir = dir('C:\\','photorepos','Perl','Mother','WorkLoad'); print "Path: ", Dumper( "$dir" ); print $dir->is_absolute ? "Is Absolute\n" : "Is Relative\n"; print "Volume: ", $dir->volume, "\n"; print "Dir List: ",Dumper( [$dir->dir_list] );

    I admit I haven't worked with volume specs in either module, and I don't have a Windows machine handy at this moment, so the above may need some tweaking in that respect.

    Update 2016-10-29: I tested this on a Windows machine and apparently, in File::Spec, the volume argument to catpath needs to be 'C:', not just 'C', while in Path::Class, the volume needs to be specified as 'C:\\' for things to work properly (unfortunately it seems neither of these points are mentioned in the docs). I updated the above code accordingly and added some more output. The Path::Class code does not work the same under Linux as under Windows: since in Linux "C:\" is a valid name for a directory, that path component is treated like just another regular directory. Here are the outputs:

    # Windows "\\photorepos\\Perl\\Mother\\WorkLoad" "C:\\photorepos\\Perl\\Mother\\WorkLoad" "C:\\photorepos\\Perl\\Mother\\WorkLoad" Path: "C:\\photorepos\\Perl\\Mother\\WorkLoad" Is Absolute Volume: C: Dir List: ["", "photorepos", "Perl", "Mother", "WorkLoad"] # Linux "/photorepos/Perl/Mother/WorkLoad" "/photorepos/Perl/Mother/WorkLoad" "C:\\/photorepos/Perl/Mother/WorkLoad" Path: "C:\\/photorepos/Perl/Mother/WorkLoad" Is Relative Volume: Dir List: ["C:\\", "photorepos", "Perl", "Mother", "WorkLoad"]

    Hope this helps,
    -- Hauke D

      Give Path::Tiny a look. I was a fan of Path::Class from its first releases but I think PT is quite a bit better.

        Hi Your Mother,

        I've read many good things about Path::Tiny but haven't had the chance to really use it. One thing that held me back a little from switching from ::Class to ::Tiny is that Path::Class uses File::Spec internally, while Path::Tiny apparently does not (or only partially) - its doc says:

        Path::Tiny does not try to work for anything except Unix-like and Win32 platforms. Even then, it might break if you try something particularly obscure or tortuous. ... All paths are forced to have Unix-style forward slashes.

        So at the moment it feels a little safer and more portable to stick with Path::Class - not a particularly good reason, I know :-) So I'll definitely try out Path::Tiny when I get the chance.

        -- Hauke D

Re: moving from mac to PC
by Laurent_R (Canon) on Oct 28, 2016 at 06:22 UTC
    In addition to the backslash issue mentioned above, your $findme path probably does not make sense on a PC running Windows.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1174864]
Approved by Laurent_R
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-05-25 16:59 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (90 votes). Check out past polls.