in reply to including variables

What would you guys say about this solution?: This file is called : "include.pl"
use strict; use warnings; include('test.pl') > 2 or die('Couldnt include important file!'); # This sub returns the number of bytes read # or a negative number if there was an error... sub include { my $FILE = shift; -e $FILE or return -1; # File doesn't exist -f $FILE or return -2; # not a plain file my $SIZE = -s $FILE; $SIZE > 2 or return $SIZE; # File is basically empty my $CONTENT; my $F; open $F, '<:raw', $FILE or return -3; # Can't open file binmode $F; sysread($F, $CONTENT, $SIZE) == $SIZE or return -4; # Can't read close $F; eval($CONTENT); return $SIZE; } ShowMessage(); ChangeMessage('Whats up!'); ShowMessage(); exit;
and this file is called "test.pl" :
my $MESSAGE = 'Hello World!!!'; # This sub displays the $MESSAGE: sub ShowMessage { print "$MESSAGE\n"; } sub ChangeMessage { $MESSAGE = shift; }

Replies are listed 'Best First'.
Re^2: including variables
by haukex (Archbishop) on Feb 17, 2018 at 09:19 UTC

    Note that your sub include appears to essentially be a re-implementation of do.

Re^2: including variables
by Anonymous Monk on Feb 16, 2018 at 22:50 UTC
    Evil low level c style?! If hou gotta have perl data include file do without eval use Safest undumper