Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Perl / June 2004



Tip: Looking for answers? Try searching our database.

n00b needs help with file arguments

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ian Ferguson - 31 May 2004 16:08 GMT
Hello folks. I was wondering if somebody could help me out and let me know
how to solve this problem.

I am trying to write an interactive script that prompts the user to specify
a path to a DMG file on their system. It seems that once a file is inputted
via drag and drop in this manner, the file does not seem to exist to the
script. I figured at first this may be due to the shell escaping the input,
but removing these escapes did not help matters. Here is what I built to
describe my issue (tested with directories w/ spaces on purpose):

#!/usr/bin/perl

sub line { print "-" x 50, "\n"; }

# Path as script argument
my $fileOne = $ARGV[0];

# Path as user input
print "Enter file: ";
chomp(my $fileTwo = <STDIN>);

# Path as explicit
my $fileThree = "/Users/my_username/Desktop/with spaces/IAOD.dmg";

# Another test to remove the escapes from the shell
my $fileFour = $fileTwo;
$fileFour =~ s/\\//g;

print "\nDumping paths\n";
&line;
print <<EOF;
\$fileOne => $fileOne
\$fileTwo => $fileTwo
\$fileThree => $fileThree
\$fileFour => $fileFour
EOF
print "\nTesting formats:\n";
&line;
print -f $fileOne ? "\$fileOne => exists\n"
 : "\$fileOne => does not exist\n";
print -f $fileTwo ? "\$fileTwo => exists\n"
 : "\$fileTwo => does not exist\n";
print -f $fileThree ? "\$fileThree => exists\n"
 : "\$fileThree => does not exist\n";
print -f $fileFour ? "\$fileFour => exists\n"
 : "\$fileFour => does not exist\n";

__DATA__
prompt$: perl test /Users/my_username/Desktop/with\ spaces/IAOD.dmg
Enter file: /Users/my_username/Desktop/with\ spaces/IAOD.dmg

Dumping paths
--------------------------------------------------
$fileOne => /Users/my_username/Desktop/with spaces/IAOD.dmg
$fileTwo => /Users/my_username/Desktop/with\ spaces/IAOD.dmg
$fileThree => /Users/my_username/Desktop/with spaces/IAOD.dmg
$fileFour => /Users/my_username/Desktop/with spaces/IAOD.dmg

Testing formats:
--------------------------------------------------
$fileOne => exists
$fileTwo => does not exist
$fileThree => exists
$fileFour => does not exist

--
Ian Ferguson
Toronto, Canada
mailto:theferg2004@rogers.com
Ricardo Signes - 31 May 2004 17:02 GMT
* Ian Ferguson <theferg2003@rogers.com> [2004-05-31T11:08:48]
> I am trying to write an interactive script that prompts the user to specify
> a path to a DMG file on their system. It seems that once a file is inputted
> via drag and drop in this manner, the file does not seem to exist to the
> script. I figured at first this may be due to the shell escaping the input,
> but removing these escapes did not help matters. Here is what I built to
> describe my issue (tested with directories w/ spaces on purpose):

You never tried looking carefully at the dropped filename, from what I
see.  Check it out and I believe you'll find that a space is added at
the end of the filename.  So, if you drag and drop "/foo/bar.txt" and
hit enter, you actually get "/foo/bar.txt \n".  You chomp once, but
you're left with the trailing space.  Remove that, too.

Signature

rjbs

Ian Ferguson - 31 May 2004 17:50 GMT
"Quoth Ricardo SIGNES" <rjbs@manxome.org> on 5/31/04 12:02 PM:

> You never tried looking carefully at the dropped filename, from what I
> see.  Check it out and I believe you'll find that a space is added at
> the end of the filename.  So, if you drag and drop "/foo/bar.txt" and
> hit enter, you actually get "/foo/bar.txt \n".  You chomp once, but
> you're left with the trailing space.  Remove that, too.

DOH! 8(|)

That seemed to fix it, but the escaped version still is not recognized. I
have added "*" to indicate that there are no more spaces.

I guess that this is a good time to ask whether or not Perl requires me to
quote paths containing spaces or if it is smart enough to resolve things on
its own. For example, if I had assigned $dir =
"/Users/my_username/Desktop/with spaces/" and then I was to rmdir( $dir )
would this cause a problem or should I quote the variable rmdir( "$dir" )?

Dumping paths
--------------------------------------------------
$fileOne => */Users/my_username/Desktop/with spaces/IAOD.dmg*
$fileTwo => */Users/my_username/Desktop/with\ spaces/IAOD.dmg*
$fileThree => */Users/my_username/Desktop/with spaces/IAOD.dmg*
$fileFour => */Users/my_username/Desktop/with spaces/IAOD.dmg*

Testing formats:
--------------------------------------------------
$fileOne => exists
$fileTwo => does not exist
$fileThree => exists
$fileFour => exists

--
Ian Ferguson
i:media
Toronto, Canada
mailto:theferg2004@rogers.com
Ricardo Signes - 31 May 2004 19:01 GMT
* Ian Ferguson <theferg2003@rogers.com> [2004-05-31T12:50:54]
> I guess that this is a good time to ask whether or not Perl requires me to
> quote paths containing spaces or if it is smart enough to resolve things on
> its own. For example, if I had assigned $dir =
> "/Users/my_username/Desktop/with spaces/" and then I was to rmdir( $dir )
> would this cause a problem or should I quote the variable rmdir( "$dir" )?

Step 1: consulet perldoc (perldoc -f rmdir, etc)
Step 2: Try It And See
Step 3: Ask Humans ;)

Signature

rjbs

Sherm Pendley - 31 May 2004 20:01 GMT
> That seemed to fix it, but the escaped version still is not recognized.

It's not supposed to be recognized. Escapes are necessary in a shell,
because it uses white space as a delimiter, so white space that isn't
to be treated as such needs to be escaped.

> I guess that this is a good time to ask whether or not Perl requires
> me to
> quote paths containing spaces

No, it doesn't. Is this a good time to ask what led you to believe it
might, or should? ;-)

>  or if it is smart enough to resolve things on its own.

There's nothing to resolve. The file name is (for example) "file name
space.txt". When you use that in a shell without escaping the spaces,
the shell splits it up into three separate arguments: ("file", "name",
"space.txt"). To prevent it from doing that - i.e. to tell the shell
that the spaces are part of a single argument, not delimiters between
multiple arguments - you escape the spaces.

In Perl, strings are most often delimited by single- and double-quotes,
not spaces. So spaces don't normally need to be escaped. But see
'perldoc perlop' - especially the "Quote and Quote-like Operators"
section. The point of having a variety of quote operators is to allow
you to choose a delimiter that doesn't appear in the input and thus
doesn't need to be escaped.

>  For example, if I had assigned $dir =
> "/Users/my_username/Desktop/with spaces/" and then I was to rmdir(
> $dir )
> would this cause a problem or should I quote the variable rmdir(
> "$dir" )?

It's unnecessary, and some members of the Cranky Perl Police will
chastise you for it. (See <http://tinyurl.com/2l6wm>). It *is* slightly
inefficient - a temporary string is created, and then $dir is
interpolated into the temp string - but the CPP makes entirely too big
an issue of it.

sherm--
Ian Ferguson - 31 May 2004 22:13 GMT
"Quoth Sherm Pendley" <sherm@dot-app.org> on 5/31/04 3:01 PM:

>> That seemed to fix it, but the escaped version still is not recognized.
>
[quoted text clipped - 8 lines]
> No, it doesn't. Is this a good time to ask what led you to believe it
> might, or should? ;-)

I program AppleScript as well. In fact, this is what introduced me to Perl
and Ruby. Since Perl can handle many of the things I need faster and more
efficiently, I started digging into Perl to do things like sort arrays, etc.

In AppleScript, If I have a script and I need to call on a shell script to
run with specific parameters, I have to issue a "quoted form of ..." to make
sure that the argumetn is quoted to prevent the shell from seeing various
arguments as you noted. This is what led me to ask the question regarding
quoting. I thank you for your explanation of how Perl handles things Sherm.

:)

>>  For example, if I had assigned $dir =
>> "/Users/my_username/Desktop/with spaces/" and then I was to rmdir(
[quoted text clipped - 7 lines]
> interpolated into the temp string - but the CPP makes entirely too big
> an issue of it.

** This does not apply to you Sherm **

I asked a question about quoting an argument prior to removing a directory
to learn something about how the language handles things. I don't know if
the rmdir method is recursive and/or forced in Perl.. Telling me to try and
see is a BS answer to give. Of course I could create a directory and try,
but what is the need for a community of "experts" if I can't ask a "simple"
question?

Also... Read perldoc this and that... Most people new to a language will
have no clue what you are talking about. Keep this in mind for the future.
Do you tell a person who can't drive standard ... "Hey it's a car. Just
drive it!"? Of course not. If you don't have the time to provide a proper
answer, or some code to help explain things, please don't waste your time
replying.

--
Ian Ferguson
Toronto, Canada
mailto:theferg2004@rogers.com
Joseph Alotta - 01 Jun 2004 17:36 GMT
> ** This does not apply to you Sherm **
>
[quoted text clipped - 20 lines]
> time
> replying.

I heartily agree.  People should be helpful.  In the old days of the
internet
this was more common.  Being kind is what separates a prince from a
rouge.

Besides the documentation is obscure and lacking and not easy to know
where it is found.

Joe.
Ken Williams - 02 Jun 2004 00:23 GMT
> I program AppleScript as well. In fact, this is what introduced me to
> Perl
[quoted text clipped - 12 lines]
> regarding
> quoting.

The key difference here is that AppleScript is calling the shell to
perform its actions, whereas Perl is calling native system routines.  
So no protection from the shell is necessary.

> I asked a question about quoting an argument prior to removing a
> directory
[quoted text clipped - 7 lines]
> "simple"
> question?

Well, in Perl there's more of a culture that tries things - just
because it's so much easier to write one-off simple Perl programs than
one-off Java or C or AppleScript or whatever.  So I suspect Ricardo, in
his terse way, had that in mind.

However, using the "try it and see" approach can get you in trouble
too, since people should really rely on the documentation (and a real
understanding thereof) to achieve true enlightenment.

> Also... Read perldoc this and that... Most people new to a language
> will
> have no clue what you are talking about. Keep this in mind for the
> future.
> Do you tell a person who can't drive standard ... "Hey it's a car. Just
> drive it!"? Of course not.

Right, but I don't think you're going to run over any little old ladies
with a test perl script. ;-)

 -Ken
Ian Ferguson - 02 Jun 2004 14:58 GMT
"Quoth Ken Williams" <ken@mathforum.org> on 6/1/04 7:23 PM:

>> In AppleScript, If I have a script and I need to call on a shell
>> script to
[quoted text clipped - 9 lines]
> perform its actions, whereas Perl is calling native system routines.
> So no protection from the shell is necessary.

Got you. I was not aware of the interaction behind the scenes apart from
what I needed to do to get proper results. I guess this makes sense in the
end. Thanks for clearing it up.  ;)

> However, using the "try it and see" approach can get you in trouble
> too, since people should really rely on the documentation (and a real
> understanding thereof) to achieve true enlightenment.

I agree. I am also not one to just experiment with things that have the
potential to go wrong. For playing with methods like map and grep, no
problem, but when it comes to removal of files... I have no time to take any
chances.

>> Also... Read perldoc this and that... Most people new to a language
>> will
[quoted text clipped - 7 lines]
>
>   -Ken

Lol. :)

Thanks to everybody for their help/input. Things are running great now that
I got over this little bump in the road!

--
Ian Ferguson
Toronto, Canada
mailto:theferg2004@rogers.com
Wiggins D Anconia - 01 Jun 2004 18:02 GMT
> > ** This does not apply to you Sherm **
> >
[quoted text clipped - 28 lines]
> Besides the documentation is obscure and lacking and not easy to know
> where it is found.

True, however this is a list for Mac OS X related questions, not newbie
Perl questions. The beginners@perl.org is a much better choice, and much
friendlier when it comes to such questions, as that is its focus.  RTFM
is a good answer on a topic specific list where it is assumed one
already knows how to access the M. "In the old days of the internet" it
was also more common for one to put in the required amount of effort to
find the most proper place to ask their question, which is less and less
common.

"A list for beginning Perl programmers to ask questions in a friendly
atmosphere." - beginners

"Discussion of Perl on Macintosh OS X" - macosx

http://lists.perl.org

Time to throw this out again:
http://www.catb.org/~esr/faqs/smart-questions.html

http://danconia.org
Wiggins D Anconia - 02 Jun 2004 15:23 GMT
> I think you are missing the point.  If a beginner knew the *right*
> place to find information, he would not be a beginner.

It takes more than knowing where to look to not be a beginner. I
generally know where to look and compared to many on this list and
others I am definitely very much a beginner.  

> Meditate on this and think about the time when you were a beginner
> and how someone helped you.

Right, I have, and I did the same as they did, pointed me to the correct
location. That is why I said that the beginners list was a better place
and stated why, I didn't just say get the hell out of here you newbie.

> Meditate on how the world would be if people in other professions did
> the same thing you do.  For example, you go into a doctor's office and
> he gives you a lecture on how you need to find the appropriate medical
> resource and tells you to go to the library and look it up.

Actually when I go into a doctor's office I very often do get referred
to a different location, specifically to the correct specific resource
for what I am after, just because it doesn't take the form of a library
doesn't mean it isn't the same thing.  I doubt seriously you ask a
cardiologist about back pain??

http://danconia.org

> Joe.
>
[quoted text clipped - 30 lines]
> >
> > http://danconia.org
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.