Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

New Line removal from both Unix & Win32

by Anonymous Monk
on Dec 30, 2002 at 16:03 UTC ( [id://223060]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks,

I've been using the following code thanks to jmcnamara

if ($^O eq 'MSWin32') { open my $fh, "+<$Conf[1]" or warn "$!"; binmode $fh; my $size = 4096; seek $fh, -$size, 2; # Locate the end of the file while (1) { $cur_pos = tell $fh; read $fh, $buf, $size; last if $buf =~ m/\S/s; seek $fh, -$size*2, 1; } $buf =~ m/(\s*)$/s; $cur_pos += $-[0] || 0; truncate $fh, ++$cur_pos if $cur_pos; close $fh; }
I had to check for the OS because this routine fails on Unix, does anyone know why?

Thanks

Replies are listed 'Best First'.
Re: New Line removal from both Unix & Win32
by Aristotle (Chancellor) on Dec 30, 2002 at 17:07 UTC

    Please, never never use $^O eq $foo. Do a pattern match instead: $^O =~ /Win32/i. But judging from your error message I'm guessing that your Linux box is using an older version of Perl (5.005 or earlier) which doesn't autovivify filehandles so it's barfing at the open with a lexical variable. You have to write it as follows:

    use Symbol (); my $fh = Symbol::gensym(); open $fh, "+<", $Conf[1] or warn "$!";
    And please please use the three-argument from of open (as shown in my snippet).

    Makeshifts last the longest.

Re: New Line removal from both Unix & Win32
by Zaxo (Archbishop) on Dec 30, 2002 at 16:20 UTC

    What error message do you get on Unix? I don't see any obviously wrong operations.

    Is $Conf[1] a file name in non-portable winders-like form? If that's all, Perl can assist in producing more portable file name code. See perlport.

    After Compline,
    Zaxo

Re: New Line removal from both Unix & Win32
by waswas-fng (Curate) on Dec 30, 2002 at 18:17 UTC
    One more question, why open my $fh, "+<$Conf[1]" or warn "$!"; not open my $fh, "+<$Conf[1]" or die "$!"; as from the snippet you have there you have no work around for failed open -- it can't handle it so it may as well die and be safe.

    -Waswas
      The code below is no longer getting an error but is failing to remove new lines from the end of files in the Unix environment
      my $fh = Symbol::gensym(); open $fh, '+<$Conf[1]' or warn "$!"; binmode $fh; my $size = 4096; seek $fh, -$size, 2; # Locate the end of the file while (1) { $cur_pos = tell $fh; read $fh, $buf, $size; last if $buf =~ m/\S/s; seek $fh, -$size*2, 1; } $buf =~ m/(\s*)$/s; $cur_pos += $-[0] || 0; truncate $fh, ++$cur_pos if $cur_pos; close $fh;
      Any ideas why

      Thanks again

Re: New Line removal from both Unix & Win32
by Nitrox (Chaplain) on Dec 30, 2002 at 16:16 UTC
    Without being provided any error messages I can only speculate that Unix file permissions may be coming into play.

    -Nitrox

      Sorry,

      The error is:

      Can't use an undefined value as a symbol reference at test.pl.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-25 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found