in reply to Re^2: First attempt at bringing in file for input/output
in thread First attempt at bringing in file for input/output

Perl 5.6, which introduced the 3-argument open, was released more than 18 years ago. AFAIK people being stuck on a Perl <5.6 should be pretty rare nowadays.

I have Perl 5.004 for DOS on my computer. I am not stuck on it. This is not what I use most of the time, but I do run it from time to time. There's nothing wrong with having an old perl interpreter. Right? Here's proof:

C:\>perl -v This is perl, version 5.004_02 Copyright 1987-1997, Larry Wall MS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996 djgpp v2 port (perl5004) by Laszlo Molnar, 1997 Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5.0 source +kit. C:\>

When I fire up good old MS-DOS, this is the perl interpreter I use. So, if I write a perl script and want to run it in DOS mode, then it won't run unless I make sure it's compatible with Perl 5.004. If I can write a perl script that is compatible with DOS and I know how to do it, then why not do it? I want my scripts to be compatible.

The two-argument form of open will invoke external programs if the first or last character of the filename is a pipe (|). If the filename is user-supplied, this is a security risk!

Wow. That's good to know! Thank you for mentioning this!

Your code won't run on a Perl <5.6 because warnings wasn't introduced until 5.6.

Yes, I overcame that problem by simply creating a "warnings.pm" file in the LIB directory, so perl 5.004 thinks it's there. and doesn't give me an error anymore. So, I just want perl to shut up and run my scripts without any errors. I've achieved that. And as I said, the only thing that makes my scripts incompatible now is that I use open() with 3 args instead of 2. But using 2 args is no biggie. I can do that easily. which means my scripts are compatible all the way back to DOS era! Hehe

"Notice that you do not need to use a variable for file handle. You can use numbers." What advantage does this have? (Especially to a beginner?)

The first programming language I learned was QBASIC. To open a file in QBASIC, you use numbers as file handles. See example here:

OPEN FILENAME1$ FOR INPUT AS #1 OPEN FILENAME2$ FOR OUTPUT AS #2 LINE INPUT #1, ROW$ PRINT #2, ROW$ CLOSE #2 CLOSE #1 END

I don't know about you, but the above code looks very familiar to me, because I have written this a lot. The fact that I can use a number as a file handle in Perl is VERY COOL. It makes my perl code look more familiar to me. :)

Replies are listed 'Best First'.
Re^4: First attempt at bringing in file for input/output
by haukex (Archbishop) on Nov 01, 2018 at 12:01 UTC

    In the context of running Perl 5.004 on DOS, all that is fine, of course, after all, there is more than one way to do it. I've written modules that I try to make as backwards-compatible as possible (although only down to 5.6, so far). I even wrote some QBASIC way back. So if it helps you, you're of course free to code that way (setting aside for a moment the usual arguments about future maintainability and so on). I might just suggest that your fake warnings.pm at least contain $^W=1;, and to be aware of the issues of two-arg open like I mentioned.

    On the other hand, your post was a reply to a wisdom seeker who said they were just getting started (and not necessarily on 5.004 for DOS :-) ). Many of the suggestions you made go against modern best practices (useful variable names, don't use $a and $b outside of sort, three-arg open with lexical filehandles, using warnings, and using a recent version of Perl), and those best practices usually exist for a good reason. So I think it's important to be aware of that, and I'd suggest putting in a disclaimer.

    Other than that, getting Perl to run in as many places as possible is certainly not a bad thing :-)

Re^4: First attempt at bringing in file for input/output
by poj (Abbot) on Nov 01, 2018 at 12:52 UTC

    How do you print to a numeric filehandle. Is it using select ?

    poj
      No, unfortunately I can't make select work. I don't know why. I use sysread() and syswrite().
      #!/usr/bin/perl use strict; use warnings; my $in = '/bootlog.txt'; my $out = 'bootlog2.txt'; -e $in or die "Input file doesn't exist - $in"; -f $in or die "Input file is not a plain file - $in"; my $size = -s $in; $size > 0 or die "Input file is empty - $in"; my $BUFF; open 0, "< $in" or die "Can't open input file - $in"; binmode 0; sysread(0, $BUFF, $size) == $size or die "Can't read file - $in"; close 0 or die "Can't close input file - $in"; open 0, "> $out" or die "Can't create file - $out"; binmode 0; syswrite(0, $BUFF, length($BUFF), 0); close 0 or die "Can't close output file - $out"; print( (-s $out) . " bytes written.\n"); print( (-s $out == $size ? 'SUCCESS' : 'FAILED') . "\n\n"); exit;