When Returning 2 or more values from a Subroutine only the last varaible returns defined





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm currently writing a small Perl script to act as a cronjob.



The task of the Subroutinen is to check whether the file I'm writing to is a valid json file or not and to then proceed based on the return of this subroutine and the output of another Subroutine.



Even though I do return the multiple values in a list and then assign the result of the function to the list of variables that i want define, it only seams to work with the last value from the list.



I could just encapsulated the values with an array, but i want to knowe why the method i tried is not functioning as i expected.



(sorry for the bad formatting...)



#!/usr/bin/perl

use strict;
use warnings;
use JSON;
use Data::Dumper;


sub is_valid_json {

my $readJson = do {
open(my $readJson, "+<", "../data_out.json")
or die("Can't open $filename": $!n");
local $/;
<$readJson>
};

my $json = JSON->new;

eval {

my $result = $json->decode($readJson);
my $JsonValidationStatus = 1;

return($JsonValidationStatus,$readJson );
} or do {
warn "Failed to decode_json result";
print "mission failled";
my $JsonValidationStatus = 0;
return($JsonValidationStatus, $readJson);
};
}

my ($JsonValidationStatus, $readJson) = is_valid_json();
print "One:". Dumper "$JsonValidationStatusn";
print "Two:" . Dumper "$readJsonn";
print "n n n n";


ps. sorry forgot to provide the error message:




Use of uninitialized value $readJson in concatenation (.)











share|improve this question




















  • 8





    return inside an eval returns from the eval, not the subroutine.

    – Matt Jacob
    Jan 4 at 15:05











  • now i just feel stupid... thanks anyway... should have known it better.

    – Lost Demondragon
    Jan 4 at 15:17






  • 1





    Irrelevant to your question, but I think you could use Data::Dumper more gracefully: print Data::Dumper->Dump([$JsonValidationStatus, $readJson], ['*One', '*Two']);

    – Ed Grimm
    Jan 6 at 0:27











  • Thankful for any help that might improve upon my code/coding style :D... Indeed i have to confess that you're way of writing is a lot more smoother and sexier. Am and Matt Jacob would you like to make you're coment the answer of this thread. I cant axept it if its just a coment, and posting the answer my self would be injust.

    – Lost Demondragon
    Jan 8 at 11:43


















1















I'm currently writing a small Perl script to act as a cronjob.



The task of the Subroutinen is to check whether the file I'm writing to is a valid json file or not and to then proceed based on the return of this subroutine and the output of another Subroutine.



Even though I do return the multiple values in a list and then assign the result of the function to the list of variables that i want define, it only seams to work with the last value from the list.



I could just encapsulated the values with an array, but i want to knowe why the method i tried is not functioning as i expected.



(sorry for the bad formatting...)



#!/usr/bin/perl

use strict;
use warnings;
use JSON;
use Data::Dumper;


sub is_valid_json {

my $readJson = do {
open(my $readJson, "+<", "../data_out.json")
or die("Can't open $filename": $!n");
local $/;
<$readJson>
};

my $json = JSON->new;

eval {

my $result = $json->decode($readJson);
my $JsonValidationStatus = 1;

return($JsonValidationStatus,$readJson );
} or do {
warn "Failed to decode_json result";
print "mission failled";
my $JsonValidationStatus = 0;
return($JsonValidationStatus, $readJson);
};
}

my ($JsonValidationStatus, $readJson) = is_valid_json();
print "One:". Dumper "$JsonValidationStatusn";
print "Two:" . Dumper "$readJsonn";
print "n n n n";


ps. sorry forgot to provide the error message:




Use of uninitialized value $readJson in concatenation (.)











share|improve this question




















  • 8





    return inside an eval returns from the eval, not the subroutine.

    – Matt Jacob
    Jan 4 at 15:05











  • now i just feel stupid... thanks anyway... should have known it better.

    – Lost Demondragon
    Jan 4 at 15:17






  • 1





    Irrelevant to your question, but I think you could use Data::Dumper more gracefully: print Data::Dumper->Dump([$JsonValidationStatus, $readJson], ['*One', '*Two']);

    – Ed Grimm
    Jan 6 at 0:27











  • Thankful for any help that might improve upon my code/coding style :D... Indeed i have to confess that you're way of writing is a lot more smoother and sexier. Am and Matt Jacob would you like to make you're coment the answer of this thread. I cant axept it if its just a coment, and posting the answer my self would be injust.

    – Lost Demondragon
    Jan 8 at 11:43














1












1








1








I'm currently writing a small Perl script to act as a cronjob.



The task of the Subroutinen is to check whether the file I'm writing to is a valid json file or not and to then proceed based on the return of this subroutine and the output of another Subroutine.



Even though I do return the multiple values in a list and then assign the result of the function to the list of variables that i want define, it only seams to work with the last value from the list.



I could just encapsulated the values with an array, but i want to knowe why the method i tried is not functioning as i expected.



(sorry for the bad formatting...)



#!/usr/bin/perl

use strict;
use warnings;
use JSON;
use Data::Dumper;


sub is_valid_json {

my $readJson = do {
open(my $readJson, "+<", "../data_out.json")
or die("Can't open $filename": $!n");
local $/;
<$readJson>
};

my $json = JSON->new;

eval {

my $result = $json->decode($readJson);
my $JsonValidationStatus = 1;

return($JsonValidationStatus,$readJson );
} or do {
warn "Failed to decode_json result";
print "mission failled";
my $JsonValidationStatus = 0;
return($JsonValidationStatus, $readJson);
};
}

my ($JsonValidationStatus, $readJson) = is_valid_json();
print "One:". Dumper "$JsonValidationStatusn";
print "Two:" . Dumper "$readJsonn";
print "n n n n";


ps. sorry forgot to provide the error message:




Use of uninitialized value $readJson in concatenation (.)











share|improve this question
















I'm currently writing a small Perl script to act as a cronjob.



The task of the Subroutinen is to check whether the file I'm writing to is a valid json file or not and to then proceed based on the return of this subroutine and the output of another Subroutine.



Even though I do return the multiple values in a list and then assign the result of the function to the list of variables that i want define, it only seams to work with the last value from the list.



I could just encapsulated the values with an array, but i want to knowe why the method i tried is not functioning as i expected.



(sorry for the bad formatting...)



#!/usr/bin/perl

use strict;
use warnings;
use JSON;
use Data::Dumper;


sub is_valid_json {

my $readJson = do {
open(my $readJson, "+<", "../data_out.json")
or die("Can't open $filename": $!n");
local $/;
<$readJson>
};

my $json = JSON->new;

eval {

my $result = $json->decode($readJson);
my $JsonValidationStatus = 1;

return($JsonValidationStatus,$readJson );
} or do {
warn "Failed to decode_json result";
print "mission failled";
my $JsonValidationStatus = 0;
return($JsonValidationStatus, $readJson);
};
}

my ($JsonValidationStatus, $readJson) = is_valid_json();
print "One:". Dumper "$JsonValidationStatusn";
print "Two:" . Dumper "$readJsonn";
print "n n n n";


ps. sorry forgot to provide the error message:




Use of uninitialized value $readJson in concatenation (.)








perl subroutine






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 15:07







Lost Demondragon

















asked Jan 4 at 15:00









Lost DemondragonLost Demondragon

287




287








  • 8





    return inside an eval returns from the eval, not the subroutine.

    – Matt Jacob
    Jan 4 at 15:05











  • now i just feel stupid... thanks anyway... should have known it better.

    – Lost Demondragon
    Jan 4 at 15:17






  • 1





    Irrelevant to your question, but I think you could use Data::Dumper more gracefully: print Data::Dumper->Dump([$JsonValidationStatus, $readJson], ['*One', '*Two']);

    – Ed Grimm
    Jan 6 at 0:27











  • Thankful for any help that might improve upon my code/coding style :D... Indeed i have to confess that you're way of writing is a lot more smoother and sexier. Am and Matt Jacob would you like to make you're coment the answer of this thread. I cant axept it if its just a coment, and posting the answer my self would be injust.

    – Lost Demondragon
    Jan 8 at 11:43














  • 8





    return inside an eval returns from the eval, not the subroutine.

    – Matt Jacob
    Jan 4 at 15:05











  • now i just feel stupid... thanks anyway... should have known it better.

    – Lost Demondragon
    Jan 4 at 15:17






  • 1





    Irrelevant to your question, but I think you could use Data::Dumper more gracefully: print Data::Dumper->Dump([$JsonValidationStatus, $readJson], ['*One', '*Two']);

    – Ed Grimm
    Jan 6 at 0:27











  • Thankful for any help that might improve upon my code/coding style :D... Indeed i have to confess that you're way of writing is a lot more smoother and sexier. Am and Matt Jacob would you like to make you're coment the answer of this thread. I cant axept it if its just a coment, and posting the answer my self would be injust.

    – Lost Demondragon
    Jan 8 at 11:43








8




8





return inside an eval returns from the eval, not the subroutine.

– Matt Jacob
Jan 4 at 15:05





return inside an eval returns from the eval, not the subroutine.

– Matt Jacob
Jan 4 at 15:05













now i just feel stupid... thanks anyway... should have known it better.

– Lost Demondragon
Jan 4 at 15:17





now i just feel stupid... thanks anyway... should have known it better.

– Lost Demondragon
Jan 4 at 15:17




1




1





Irrelevant to your question, but I think you could use Data::Dumper more gracefully: print Data::Dumper->Dump([$JsonValidationStatus, $readJson], ['*One', '*Two']);

– Ed Grimm
Jan 6 at 0:27





Irrelevant to your question, but I think you could use Data::Dumper more gracefully: print Data::Dumper->Dump([$JsonValidationStatus, $readJson], ['*One', '*Two']);

– Ed Grimm
Jan 6 at 0:27













Thankful for any help that might improve upon my code/coding style :D... Indeed i have to confess that you're way of writing is a lot more smoother and sexier. Am and Matt Jacob would you like to make you're coment the answer of this thread. I cant axept it if its just a coment, and posting the answer my self would be injust.

– Lost Demondragon
Jan 8 at 11:43





Thankful for any help that might improve upon my code/coding style :D... Indeed i have to confess that you're way of writing is a lot more smoother and sexier. Am and Matt Jacob would you like to make you're coment the answer of this thread. I cant axept it if its just a coment, and posting the answer my self would be injust.

– Lost Demondragon
Jan 8 at 11:43












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54041420%2fwhen-returning-2-or-more-values-from-a-subroutine-only-the-last-varaible-returns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54041420%2fwhen-returning-2-or-more-values-from-a-subroutine-only-the-last-varaible-returns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas