Morgan Jones on 25 Jun 2009 10:14:20 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] question re: perl diamond operator


You could also loop through %ARGV and test each value.

http://en.wikipedia.org/wiki/There%27s_more_than_one_way_to_do_it

-morgan


On Jun 25, 2009, at 1:03 PM, Mark M. Hoffman wrote:

> Hi:
>
> * Morgan Jones <morgan@morganjones.org> [2009-06-25 12:47:38 -0400]:
>> #! /usr/bin/perl -w
>>
>> if (! -f $ARGV[0]) {
>>     print "$ARGV[0] does not exist\n";
>>     exit 1;
>> }
>>
>> while (<>) {
>> 	chomp;
>> 	print $_;
>> }
>
> Sure I could do that, but consider...
>
> 	$ ./example.pl this_file_exists but_not_this_one
>
> It's a bit indirect, but I've solved it a completely different way.   
> I added
> the following line near the top... it installs a warning handler  
> which just
> converts all warnings into fatal errors.  Good enough for me.
>
> 	local $SIG{__WARN__} = sub { die $_[0] };
>
>> On Jun 25, 2009, at 12:24 PM, Mark M. Hoffman wrote:
>>
>>> Hi all:
>>>
>>> Given the following trivial script:
>>>
>>>>> #! /usr/bin/perl -w
>>>>>
>>>>> while (<>) {
>>>>> 	chomp;
>>>>> 	print $_;
>>>>> }
>>>
>>> 	$ ./example.pl foo
>>> 	Can't open foo: No such file or directory at ./example.pl line 3.
>>>
>>> So far so good; the file foo really does not exist.
>>>
>>> 	$ echo $?
>>> 	0
>>>
>>> Really?  I want the exit status to be non-zero here.  I've looked
>>> through the
>>> perl documentation; I can't see any way to get the diamond operator
>>> to do what
>>> I want in this case.  I would appreciate any suggestions from perl
>>> experts.
>
> Thanks & regards,
>
> -- 
> Mark M. Hoffman
> mhoffman@lightlink.com
>
> ___________________________________________________________________________
> Philadelphia Linux Users Group         --        http://www.phillylinux.org
> Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
> General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug